package com.htudata.quartz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.htudata.alarm.service.AlarmShowService;
import com.htudata.common.enums.PageSeqEnum;
import com.htudata.core.util.operation.StringOperationUtils;
import com.htudata.flow.service.FlowShowService;
import com.htudata.live.service.LiveService;
import com.htudata.transctFlow.service.TransctFlowShowService;
import com.htudata.websocket.endpoint.AgentServerEndpoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Slf4j
@EnableScheduling
@Component
public class QuartzConfiguration {

  @Autowired
  private LiveService liveService;
  
  @Autowired
  private AlarmShowService alarmShowService;
  
  @Autowired
  private FlowShowService flowShowService;
  
  @Autowired
  private TransctFlowShowService transctFlowShowService;
  
  @Scheduled(cron = "${cron.getDataTypes}")
  public void getDataTypes() {
    Map<String, String> appCodeMap = new HashMap<>();
    appCodeMap.put(PageSeqEnum.SEQ_001.getCode(), "");
    try {
      String json = this.liveService.getDataTypes(true, "");
      if (StringOperationUtils.isValidStr2(json))
        AgentServerEndpoint.sendAll(json, appCodeMap, null);
    } catch (Exception e) {
       log.error("getDataTypes",e);
    } 
  }
  
  @Scheduled(cron = "${cron.getAlarms}")
  public void getAlarms() {
      try {
          this.alarmShowService.sendAlarms();
      } catch (Exception e) {
        log.error("getAlarms",e);
      }
  }
  
  @Scheduled(cron = "${cron.get3DFlows}")
  public void get3DFlows()   {
      try {
          this.flowShowService.send3DFlows();
      } catch (Exception e) {
        log.error("get3DFlows",e);
      }
  }
  
  @Scheduled(cron = "${cron.getInternal3DFlows}")
  public void getInternal3DFlows() {
      try {
          this.flowShowService.sendInternal3DFlows();
      } catch (Exception e) {
          throw new RuntimeException(e);
      }
  }
  
  @Scheduled(cron = "${cron.getTransctFlows}")
  public void getTransctFlows() {
    Map<String, String> appCodeMap = new HashMap<>();
    appCodeMap.put(PageSeqEnum.SEQ_001.getCode(), "");
    try {
      List<Integer> statusList = new ArrayList<>();
      statusList.add(Integer.valueOf(1));
      String json = this.transctFlowShowService.getTransctFlows(false, statusList);
      if (StringOperationUtils.isValidStr2(json))
        AgentServerEndpoint.sendAll(json, appCodeMap, null); 
    } catch (Exception e) {
      log.error("getTransctFlows",e);
    } 
  }
  
  @Scheduled(cron = "${cron.getTransctFlow}")
  public void getTransctFlow() {
      try {
          this.transctFlowShowService.send3DFlows();
      } catch (Exception e) {
        log.error("getTransctFlow",e);
      }
  }
  
  @Scheduled(cron = "${cron.getFlowHealthStatis}")
  public void getFlowHealthStatis() {
      try {
          this.flowShowService.sendFlowHealthStatis();
      } catch (Exception e) {
        log.error("getFlowHealthStatis",e);
      }
  }
  
  @Scheduled(cron = "${cron.getFlowHealthRanking}")
  public void getFlowHealthRanking() {
      try {
          this.flowShowService.sendFlowHealthRanking();
      } catch (Exception e) {
        log.error("getFlowHealthRanking",e);
      }
  }
  
  @Scheduled(cron = "${cron.getTransctFlowHealthStatis}")
  public void getTransctFlowHealthStatis() {
    Map<String, String> appCodeMap = new HashMap<>();
    appCodeMap.put(PageSeqEnum.SEQ_001.getCode(), "");
    try {
      String json = this.transctFlowShowService.getTransctFlowHealthStatis(true);
      if (StringOperationUtils.isValidStr2(json))
        AgentServerEndpoint.sendAll(json, appCodeMap, null); 
    } catch (Exception e) {
      log.error("getTransctFlowHealthStatis",e);
    } 
  }
  
  @Scheduled(cron = "${cron.getTransctFlowHealthRanking}")
  public void getTransctFlowHealthRanking() {
      try {
          this.transctFlowShowService.sendTransctFlowHealthRanking();
      } catch (Exception e) {
        log.error("getTransctFlowHealthRanking",e);
      }
  }
}
